//
//  MNDReturnListHeaderView.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/16.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDReturnListHeaderView.h"
#import "NSDate+JKExtension.h"
#import "UIImageView+JKAddition.h"

@interface MNDReturnListHeaderView ()

@property (strong, nonatomic) UIButton *expandButton;
@property (strong, nonatomic) UIImageView *expandArrow;

@end

@implementation MNDReturnListHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self makeContentConstraints];
    }
    
    return self;
}

- (void)setModel:(MNDReturnListSectionModel *)model {
    _model = model;
    
    NSDate *date = [NSDate jk_dateWithString:model.dayString format:@"yyyy-MM-dd"];
    self.dateLabel.text = [NSString stringWithFormat:@"%ld月%ld日", (unsigned long)date.jk_month, (unsigned long)date.jk_day];
    self.returnMoneyLabel.text = _model.returnMoney;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isExpand) {
            
            self.expandArrow.transform = CGAffineTransformIdentity;
            
        }else{
            
            self.expandArrow.transform = CGAffineTransformMakeRotation(M_PI);
        }
        
    }];
}

- (void)expandAction:(UIButton *)sender {
    self.model.isExpand = !self.model.isExpand;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isExpand) {
            
            self.expandArrow.transform = CGAffineTransformIdentity;
            
        }else{
            
            self.expandArrow.transform = CGAffineTransformMakeRotation(M_PI);
        }
    }];
    
    if (self.headerClickedBlock) {
        self.headerClickedBlock(self.model.isExpand);
    }
}

#pragma mark - 约束

- (void)makeContentConstraints {
    
    CGFloat edgMargin = 15.0;
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(edgMargin);
        make.centerY.equalTo(self.contentView);
    }];
    
    [self.expandArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-edgMargin);
        make.centerY.equalTo(self.contentView);
    }];
    
    [self.returnMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.expandArrow.mas_leading).offset(-8.0);
        make.centerY.equalTo(self.expandArrow);
    }];
    
    [self.expandButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

#pragma mark - Getter

- (UIButton *)expandButton {
    if (!_expandButton) {
        _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_expandButton addTarget:self action:@selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_expandButton];
    }
    
    return _expandButton;
}


- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [self labelWithDefaultText:@"5月1日" textColor:kColorBlack fontSize:15.0];
        [self.contentView addSubview: _dateLabel];
    }
    
    return _dateLabel;
}

- (UILabel *)returnMoneyLabel {
    if (!_returnMoneyLabel) {
        _returnMoneyLabel = [self labelWithDefaultText:@"0.00" textColor:kColorOrange fontSize:21.0];
        
        [self.contentView addSubview: _returnMoneyLabel];
    }
    
    return _returnMoneyLabel;
}

- (UIImageView *)expandArrow {
    if (!_expandArrow) {
        _expandArrow = [UIImageView jk_imageViewWithImageNamed:@"investment_detail_expand"];
        
        [self.contentView addSubview: _expandArrow];
    }
    
    return _expandArrow;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UILabel *)labelWithDefaultText:(NSString *)text textColor:(UIColor *)color fontSize:(CGFloat)size {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textColor = color;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:size];
    
    return label;
}

@end
